/* 
 * File:   ChildProcessQuitHandler.cpp
 * Author: dell
 * 
 * Created on 2012年4月9日, 下午8:42
 */

#include "ChildProcessQuitHandler.h"
#include <evhttp/HttpHandlerManager.h>
#include <evhttp/CleanerTimer.h>



namespace Rapid{
    ChildProcessQuitHandler::ChildProcessQuitHandler() :repeat(0) {
    }

    ChildProcessQuitHandler::~ChildProcessQuitHandler() {
    }
    
    bool ChildProcessQuitHandler::onProcessSignalEvent(const ProcessSignalEvent* e){
//        LOG_INFO("child, stop server, signal:%d", e->signal);
        if(repeat == 0){
            executeClose();
        }
        return true;
    }      
    
    bool ChildProcessQuitHandler::onTimerEvent(const TimerEvent* e){
        executeClose();
        return true;
    }
    
    void ChildProcessQuitHandler::executeClose(){
        HttpHandlerManager::instance.notifyCloseAll();
//        CleanerTimer::instance.cleanupAll();
//        LOG_INFO("ChildProcessSignal, getAllCount:%d, getActiveCount:%d, repeat:%d",  HttpHandlerManager::instance.getAllCount(), HttpHandlerManager::instance.getActiveCount(), repeat);
        if(HttpHandlerManager::instance.getAllCount() <= 0 || repeat > (int)(MAX_WAIT_QUIT_TIME/MAX_WAIT_QUIT_INTERVAL)){
//            HttpHandlerManager::instance.closeAll();
            Timer::stop();
            CleanerTimer::instance.stop();      
            ProcessSignal::stop();
            exit(0);
            return;
        }
        repeat++;
        Timer::start(MAX_WAIT_QUIT_INTERVAL);
        
    }    
    
    
}



